﻿using log4net;
using ServerControl.Dal;
using ServerControl.Model;
using ServerControl.WebManage.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Web.Optimization;
using ServerControl.Model.Enum;
using System.Web.UI;

namespace ServerControl.WebManage.Controllers.Admin
{
	[RoutePrefix("admin/feedback")]
	[Auth(AllowRole = Model.Enum.Role.AdminOrUser)]
	public class FeedbackController : BaseController
	{
		private ILog logger = LogManager.GetLogger("FeedbackController");

		[Route("index")]
		public ActionResult Index()
		{
			ViewBag.Types = FeedbackTypeDal.Types.Where(t => t.UserName == CurrentUser.UserName || t.UserName == "null").ToList();
			return View();
		}


		[Route("types")]
		public ActionResult Types()
		{
			return View();
		}

		/// <summary>
		/// 获取类型
		/// </summary>
		/// <returns></returns>
		[HttpPost]
		[Route("list-type")]
		public JsonResult GetFeedbackTypes()
		{
			try
			{
				var types = FeedbackTypeDal.Types.Where(t => t.UserName == CurrentUser.UserName || t.UserName == "null").ToList();
				return new JsonNetResult { Data = new SuccessResult<List<FeedbackType>> { Data = types, Msg = "" } };
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
		}

		[HttpPost]
		[Route("list")]
		public JsonResult ListPage(int page = 1, int limit = 500, string name = "", int typeId = 0)
		{
			try
			{
				int records = 0;
				var feedbacks = FeedbackDal.Select(ref records, page, limit, CurrentUser.UserName, typeId, name);
				return new JsonNetResult { Data = new SuccessResult<List<Feedback>> { Data = feedbacks, Count = records, Msg = "" } };
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
		}

		[HttpGet]
		[Route("export")]
		public ActionResult Export(string name = "", int typeId = 0)
		{

			try
			{
				int pageIndex = 1, pageSize = 100;
				int records = 0;
				var lines = new List<string>();
				var feedbacks = FeedbackDal.Select(ref records, pageIndex, pageSize, CurrentUser.UserName, typeId, name);

				while (feedbacks.Count > 0)
				{
					feedbacks.ForEach(d =>
					{

						lines.Add($"{d.Name}----{(string.IsNullOrEmpty(d.TypeName) ? " " : d.TypeName)}----{d.Remark}");
					});
					pageIndex++;
					feedbacks = FeedbackDal.Select(ref records, pageIndex, pageSize, CurrentUser.UserName, typeId, name);
				}

				var fileResult = new FileContentResult(Encoding.UTF8.GetBytes(string.Join(Environment.NewLine, lines.ToArray())), "application/octet-stream");
				fileResult.FileDownloadName = "export.txt";
				return fileResult;
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return Content("请求异常");
		}

		[HttpPost]
		[Route("add-type")]
		public JsonResult AddType(FeedbackType type)
		{
			try
			{
				type.AddTime = DateTime.Now;
				type.UserName = CurrentUser.UserName;
				var isSuccess = FeedbackTypeDal.Insert(type);

				if (isSuccess)
				{
					return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
				}
				else
				{
					return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
				}
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
		}

		[HttpPost]
		[Route("edit-type")]
		public JsonResult EditType(FeedbackType type)
		{
			try
			{
				type.UserName = CurrentUser.UserName;
				var isSuccess = FeedbackTypeDal.Update(type);

				if (isSuccess)
				{
					return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
				}
				else
				{
					return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
				}
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
		}

		[HttpPost]
		[Route("delete-type")]
		public JsonResult DeleteType(int id)
		{
			try
			{
				var isSuccess = FeedbackTypeDal.Delete(id, CurrentUser.UserName);

				if (isSuccess)
				{
					return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
				}
				else
				{
					return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
				}
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
		}

		[HttpPost]
		[Route("delete")]
		public JsonResult Delete(int id)
		{
			try
			{
				var isSuccess = FeedbackDal.Delete(id, CurrentUser.UserName);

				if (isSuccess)
				{
					return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
				}
				else
				{
					return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
				}
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
		}

		[HttpPost]
		[Route("delete-batch")]
		public JsonResult DeleteBatch(List<int> ids)
		{
			try
			{
				var isSuccess = FeedbackDal.Delete(ids, CurrentUser.UserName);

				if (isSuccess)
				{
					return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
				}
				else
				{
					return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
				}
			}
			catch (Exception ex)
			{
				logger.Error(ex);
			}

			return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
		}
	}
}